import { pokerSTatus } from "./enumConfig";
import { Poker } from "../GameView/gameModel";
import View from "./view";
import gameView from "../GameView/gameView";

const {ccclass, property} = cc._decorator;

@ccclass
export default class UIPoker extends View {
    @property(cc.Label)
    point:cc.Label = null
    @property(cc.Node)
    suitBig:cc.Node = null
    @property(cc.Node)
    suitSmall:cc.Node = null
    
    @property([cc.SpriteFrame])
    suitBigSprite:cc.SpriteFrame[] = []
    @property([cc.SpriteFrame])
    suitSmallSprite:cc.SpriteFrame[] = []
    @property([cc.SpriteFrame])
    suitOtherSprite:cc.SpriteFrame[] = []

    @property([cc.SpriteFrame])
    bgStateSprite:cc.SpriteFrame[] = []

    private _poker:Poker = null
    private view:gameView = null

    public get poker():Poker{return this._poker}

    private pointObj = {
    '1':'A',
    '2':'2',
    '3':'3',
    '4':'4',
    '5':'5',
    '6':'6',
    '7':'7',
    '8':'8',
    '9':'9',
    '10':'10',
    '11':'J',
    '12':'Q',
    '13':'K'
}

     start(){
          this.node.on('touchstart',this.touchStart,this)
          this.node.on('touchmove',this.touchMove,this)
          this.node.on('touchend',this.touchEnd,this)
          this.node.on('touchcancel',this.touchEnd,this)
     }

     onDestroy(){
          this.node.off('touchstart',this.touchStart,this)
          this.node.on('touchmove',this.touchMove,this)
          this.node.off('touchend',this.touchEnd,this)
          this.node.off('touchcancel',this.touchEnd,this)
     }

     private touchStart(){
         
     }

     private touchMove(){

     }

     private touchEnd(){
          this.view.onClickUIPoker(this)
     }

   init(poker:Poker,gameView:gameView){
          poker.bindView(this)
          this._poker = poker
          this.view = gameView
        this.point.string = this.pointObj[poker.point+1]
        this.suitSmall.getComponent(cc.Sprite).spriteFrame = this.suitSmallSprite[poker.suit]
        this.point.node.color = poker.suit%2 == 0 ? cc.Color.BLACK : new cc.Color(191,31,47,255)
       if(poker.point<10){
            this.suitBig.getComponent(cc.Sprite).spriteFrame = this.suitBigSprite[poker.suit]
       }
       else{
            this.suitBig.getComponent(cc.Sprite).spriteFrame = this.suitOtherSprite[poker.point - 10]
       }
       this.refresh()
   }

   public refresh(){
        this.setCardTatus(this.poker)
   }

   setCardTatus(poker){
     if(poker.status == pokerSTatus.OPEN){
          this.point.node.active = true
          this.suitBig.active = true
          this.suitSmall.active = true
          this.node.getComponent(cc.Sprite).spriteFrame = this.bgStateSprite[0]
     }else{
          this.point.node.active = false
          this.suitBig.active = false
          this.suitSmall.active = false
          this.node.getComponent(cc.Sprite).spriteFrame = this.bgStateSprite[1]
     }
   }


    /*******************************************************************
     * public function
     ******************************************************************/
   public isOpen():boolean{
     return this.poker.status === pokerSTatus.OPEN
   }

   public isPoint(point:number):boolean{
        return this.poker.point === point
   }


}
